﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiteDB;

namespace LiteDBWindowsFormsApplication1
{
    public partial class frmMain : Form
    {
        private const string Dbname = "data/sample.db";


        public frmMain()
        {
            InitializeComponent();
            InitData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserId.Text))
            {
                InsertUser();
            }
            else
            {
                UpdateUser();
            }
        }

        private void InsertUser()
        {
            //打开或者创建新的数据库
            using (var db = new LiteDatabase(Dbname))
            {
                //获取 customers 集合，如果没有会创建，相当于表
                var users = db.GetCollection<User>("Users");
                //创建 customers 实例
                User user = new User()
                {
                    UserId = Guid.NewGuid(),
                    UserName = txtUserName.Text,
                    CardId = txtCardId.Text,
                    CreateDateTime = DateTime.Now,
                    ValId = 1
                };
                // 将新的对象插入到数据表中，Id是自增，自动生成的
                users.Insert(user);

                InitData();
            }
        }

        private void UpdateUser()
        {
            //打开或者创建新的数据库
            using (var db = new LiteDatabase(Dbname))
            {
                //获取 customers 集合，如果没有会创建，相当于表
                var users = db.GetCollection<User>("Users");


                var result = users.Find(x => x.UserId == new Guid(txtUserId.Text)).FirstOrDefault();

                if (result == null) throw new NullReferenceException("未找到对象");
                result.UserName = txtUserName.Text;
                result.CardId = txtCardId.Text;
                //保存到数据库
                users.Update(result);

                InitData();
            }
        }

        private void InitData()
        {
            using (var db = new LiteDatabase(Dbname))
            {
                //获取 customers 集合，如果没有会创建，相当于表
                var users = db.GetCollection<User>("Users");

                //使用LINQ语法来检索
                IEnumerable<User> results = null;

                if (string.IsNullOrEmpty(txtKey.Text))
                {
                    results = users.FindAll();
                }
                else
                {
                    results = users.Find(x => x.UserName.Contains(txtKey.Text) || x.CardId.Contains(txtKey.Text));
                }

                if (results != null)
                {
                    dataGridView1.DataSource = results.OrderByDescending(item => item.CreateDateTime).ToList();
                }

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InitData();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtUserId.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
        }
    }
}
